package com.ksyun.campus.dataserver.config.zookperconfig;

import org.I0Itec.zkclient.ZkClient;
import org.I0Itec.zkclient.exception.ZkMarshallingError;
import org.I0Itec.zkclient.serialize.ZkSerializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.nio.charset.StandardCharsets;

/**
 * @author sxt
 * @version 1.0
 * @description TODO
 * @date 2023/8/9 20:50
 */
@Configuration
public class ZookeeperConfig {

    @Bean
    public ZkClient addZkClient(ZookeeperConfigProperties properties) {
        ZkClient zkClient = new ZkClient(properties.getAddress(), properties.getTimeout(), properties.getTimeout());
        zkClient.setZkSerializer(new ZkSerializer() {
            @Override
            public Object deserialize(byte[] bytes) throws ZkMarshallingError {
                return new String(bytes, StandardCharsets.UTF_8);
            }

            @Override
            public byte[] serialize(Object serializable) throws ZkMarshallingError {
                return ((String) serializable).getBytes(StandardCharsets.UTF_8);
            }
        });
        return zkClient;
    }


}
